<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="css/base.css" rel="stylesheet">
    <link href="css/head.css" rel="stylesheet">
    <link href="css/mine.css" rel="stylesheet">
</head>
<body>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/head.js"></script>
    <script src="js/upload.js"></script>
    <script src="js/Utils.js"></script>


    <div class="head"></div>

    <div class="main">
            <div class="uploadBox">
                <div class="avatar">
                </div>

            </div>


            <div class="username">
        
            </div>

            <div class="formBox">
                <div class="form">
                    <span>旧密码</span>
                    <input type="password" id="oldPwd">
                </div>
    
                <div class="form">
                    <span>新密码</span>
                    <input type="password" id="newPwd">
                </div>
    
                <div class="form">
                    <span>确认密码</span>
                    <input type="password" id="confirmPwd">
                </div>

                <div class="button" id="changePwd">修改</div>

            </div>


        </div>

    </div>

    <script>

        function checkPwd(){
            let oldPwd = $('#oldPwd').val()
            let newPwd = $('#newPwd').val()
            let confirmPwd = $('#confirmPwd').val()

            if(oldPwd == "" || newPwd == "" || confirmPwd == ""){
                alert("请填写完整")
                return false
            }

            else if(newPwd != confirmPwd){
                alert("密码不一致")
                return false
            }

            return true
        }

        $('document').ready(function(){

            hasLogin()

            let admin = getSession('admin')
            let username = admin.username

            $('.avatar').append(`
                <img src="${baseUrl + admin.avatar}" />
            `)

            $('.username').html(`
                <span>${username}</span>
            `)

            $('#changePwd').click(function(){
                    let oldPwd = $('#oldPwd').val()
                    let newPwd = $('#newPwd').val()
                    let confirmPwd = $('#confirmPwd').val()

                    if(!checkPwd())
                        return

                    let data = {
                        username,
                        oldPwd,
                        newPwd,
                    }

                    data = JSON.stringify(data)
                    $.ajax({
                        data,
                        url: baseUrl + 'admin/change_pwd.php',
                        type: 'POST',
                        success: function(res){

                            if(typeof(res) == 'string')
                                res = JSON.parse(res)
                            if(res.code == 200){
                                alert(res.msg)
                                location.href="mine.html"
                            }
                            else alert(res.msg)

                        }
                    })

                })
        })
    </script>
</body>
</html>